﻿using System;
using System.Windows.Forms;
using CalcSharp.Core;
using CalcSharp.Core.Containers;

namespace CalcSharp.MDIUI
{
    public partial class seteditor : Form
    {
        public seteditor()
        {
            InitializeComponent();
            this.set_name.Text = "@noname";
        }

        public seteditor(string name)
        {
            InitializeComponent();
            this.DrawSet(name);
        }

        private void DrawSet(string name)
        {
            ASet work = Program.loaded.Set_mem[name];
            this.set_name.Text = name;
            this.set_items.Items.Clear();
            for (int i=0; i<work.Count; i++)//foreach (double item in work)
            {
                this.set_items.Items.Add(work[i]);
            }
        }

        private void addItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            seteditor_itemedit insert = new seteditor_itemedit();
            if (MiscCoreFuncts.ShowDialog(insert) == DialogResult.OK)
            {
                int i = 0;
                foreach (string str in insert.Items)
                {
                    try
                    {
                        this.set_items.Items.Add(MiscFuncts.parsestr(str));
                        i++;
                    }
                    catch (CSException)
                    {
                        ;
                    }
                }
                MessageBox.Show("Added " + i.ToString() + " item(s)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.set_items.SelectedItems.Count > 0)
            {
                int i, j;
                seteditor_itemedit edit = new seteditor_itemedit();
                string[] its = new string[this.set_items.SelectedItems.Count];
                for (i = 0; i < its.Length; i++) its[i] = this.set_items.SelectedItems[i].ToString();
                edit.Items = its;
                if (MiscCoreFuncts.ShowDialog(edit) == DialogResult.OK)
                {
                    for (i = 0; i < edit.Items.Length; i++)
                    {
                        try
                        {
                            j = this.set_items.Items.IndexOf(this.set_items.SelectedItems[i]);
                            this.set_items.Items[j] = MiscFuncts.parsestr(edit.Items[i]);
                        }
                        catch (CSException) { ; }
                    }
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete " + this.set_items.SelectedItems.Count.ToString() + " elements ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                while (this.set_items.SelectedItems.Count > 0)
                {
                    this.set_items.Items.Remove(set_items.SelectedItems[0]);
                }
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            ASet ret;
            switch (Program.loaded.Set_mem.StorageMode)
            {
                case SetStorageType.mDouble:
                    ret = new DoubleSet();
                    break;
                case SetStorageType.mFloat:
                    ret = new FloatSet();
                    break;
                default:
                    ret = new FloatSet();
                    break;
            }
            foreach (double it in this.set_items.Items) ret.AddItem(it);
            Program.loaded.Set_mem[this.set_name.Text] = ret;
            MessageBox.Show("Set saved as: " + this.set_name.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private ASet Editor2Set()
        {
            ASet ret;
            switch (Program.loaded.Set_mem.StorageMode)
            {
                case SetStorageType.mDouble:
                    ret = new DoubleSet();
                    break;
                case SetStorageType.mFloat:
                    ret = new FloatSet();
                    break;
                default:
                    ret = new FloatSet();
                    break;
            }
            foreach (double it in this.set_items.Items) ret.AddItem(it);
            return ret;
        }

        private void sortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.loaded.Set_mem[this.set_name.Text] = this.Editor2Set().Sort();
            this.DrawSet(this.set_name.Text);
            //MessageBox.Show("Set saved as: " + this.set_name.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void uniqueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.loaded.Set_mem[this.set_name.Text] = this.Editor2Set().Unique();
            this.DrawSet(this.set_name.Text);
            //MessageBox.Show("Set saved as: " + this.set_name.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void reverseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.loaded.Set_mem[this.set_name.Text] = this.Editor2Set().Reverse();
            this.DrawSet(this.set_name.Text);
            //MessageBox.Show("Set saved as: " + this.set_name.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void randomitzeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.loaded.Set_mem[this.set_name.Text] = this.Editor2Set().Randomize();
            this.DrawSet(this.set_name.Text);
            //MessageBox.Show("Set saved as: " + this.set_name.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
